﻿using System;

namespace MLSharp.Filters
{
	/// <summary>
	/// Provides a base class that attribute filters can derive from.  This
	/// class handles validating and storing a list of attributes that can be used
	/// by derived classes.
	/// </summary>
	public abstract class AttributeFilterBase : IDataSetFilter
	{
		#region Proctected Properties

		/// <summary>
		/// The names of attributes passed to the filter.
		/// </summary>
		public string[] AttributeNames { get; set; }

		#endregion

		#region Public Constructors

		/// <summary>
		/// Filters the specified attributes.
		/// </summary>
		/// <param name="attributeNames">The names of attributes to remove (this parameter is case sensitive).</param>
		public AttributeFilterBase(params string[] attributeNames)
		{
			if (attributeNames == null)
			{
				AttributeNames = new string[0];
			}
			else
			{
				string[] copy = (string[])attributeNames.Clone();
				Array.Sort(copy);

				for (int i = 0; i < copy.Length - 1; i++)
				{
					if (copy[i] == copy[i + 1])
						throw (new ArgumentException("Duplicate attribute found: " + copy[i]));
				}

				AttributeNames = attributeNames;
			}
		}

		#endregion

		#region Public Methods 

		/// <summary>
		/// Applies the filter to the specified data set.
		/// </summary>
		/// <param name="dataSet">The data set to transform.</param>
		public abstract void Transform(IDataSet dataSet);

		#endregion
	}
}
